<?php
/**
 * Hellos Model for AdminAccounts Component
 *
 * @package    AdminAccounts
 * @subpackage Components
 * @license        GNU/GPL
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

/**
 * AdminAccounts Model
 *
 * @package    AdminAccounts
 * @subpackage Components
 */
class AccountsModelAccounts extends JModel
{
    /**
     * AdminAccounts data array
     *
     * @var array
     */
    var $_data;

    /**
   * Items total
   * @var integer
   */
  var $_total = null;

  /**
   * Items fields
   * @var array
   */
  var $_fields = null;

  /**
   * Pagination object
   * @var object
   */
  var $_pagination = null;

    function __construct()
  {
        parent::__construct();

        global $mainframe, $option;

        // Get pagination request variables
        $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = $mainframe->getUserStateFromRequest($option.'.limitstart', 'limitstart', 0, 'int');
        $filter_type = $mainframe->getUserStateFromRequest( "$option.filter_type", 'filter_type', 0, 'int' );
        $search	= $mainframe->getUserStateFromRequest( "$option.search",'search','', 'string' );

        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

        $this->setState('limit',	$limit);
        $this->setState('limitstart',	$limitstart);
        $this->setState('filter_type',	$filter_type);
        $this->setState('search',	$search);
  }

    /**
     * Returns the query
     * @return string The query to be used to retrieve the rows from the database
     */
    function _buildQuery()
    {
        $query = ' SELECT * '
            . ' FROM #__accounts '
        ;
        if($this->getState('filter_type')) $query .= $this->_getFilter();
        return $query;
    }

    /**
     * Retrieves the hello data
     * @return array Array of objects containing the data from the database
     */
    function getData()
    {
        // Lets load the data if it doesn't already exist
        if (empty( $this->_data ))
        {
			$limit = JRequest::getVar('limit', 50);
			$limitstart = JRequest::getVar('limitstart', 0);
			$query = $this->_buildQuery();
			$total = $this->_getListCount( $query );
			$this->_data = $this->_getList( $query, $limitstart, $limit );
        }
        JRequest::setVar( 'total', $total );
        JRequest::setVar( 'limit', $limit );
		JRequest::setVar( 'limitstart', $limitstart );

		//JError::raiseWarning('131', $query );
        return $this->_data;
    }

  function getTotal()
  {
        // Load the content if it doesn't already exist
        if (empty($this->_total)) {
            $query = $this->_buildQuery();
            $this->_total = $this->_getListCount($query);
        }
        return $this->_total;
  }

  function getPagination()
  {
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
  }

	function _getFilter(){
		$search=	$this->getState('search');
		if($search || $search != ''){
			if(!is_numeric( $search )){
				$search = str_replace(' ', '%', $search);
				$search = '\'%'.$search.'%\'';
			}
			$_filt_text = $this->_fields[$this->getState('filter_type')];
			$filter = 'WHERE'.' '.$_filt_text->text.' '.'LIKE'.' '.$search;
		}
		return $filter;
	}

	 function getFields()
  {
        // if data hasn't already been obtained, load it
        if (empty($this->_fields)) {
            $db				=& JFactory::getDBO();
            // get list of Fields for dropdown filter
            $query = 'SHOW COLUMNS FROM'
            			. ' #__accounts'
            ;
            $db->setQuery( $query );
            $this->_fields[] 		= JHTML::_('select.option',  '0', '- '. JText::_( 'Select Field' ) .' -' );
            if(!$cols = $db->loadObjectList()){
            	$this->setError($db->getErrorMsg());
            	return;
            }
            $i = 1;
            foreach ($cols as $a)
            {
            	$this->_fields[]= 	JHTML::_('select.option',  $i++, $a->Field );
            }
        }
		return $this->_fields;
  }
}
